package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.Progress;
import com.ruoyi.system.domain.Project;
import com.ruoyi.system.service.IProgressService;
import com.ruoyi.system.service.IProjectService;
import com.ruoyi.system.service.impl.SendSmsImpl;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Task;
import com.ruoyi.system.service.ITaskService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2024-04-28
 */
@RestController
@RequestMapping("/system/task")
public class TaskController extends BaseController
{
    @Autowired
    private ITaskService taskService;

    @Autowired
    private IProgressService progressService;

    @Autowired
    private SendSmsImpl sendSmsImpl;

    @Autowired
    private IProjectService projectService;
    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:task:list')")
    @GetMapping("/list")
    public TableDataInfo list(Task task)
    {
        startPage();
        List<Task> list = taskService.selectTaskList(task);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:task:export')")
    @Log(title = "【增量数据导出】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Task task)
    {
        List<Task> list = taskService.selectTaskList(task);
        ExcelUtil<Task> util = new ExcelUtil<>(Task.class);
        util.exportExcel(response, list, "任务数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:task:query')")
    @GetMapping(value = "/{taskId}")
    public AjaxResult getInfo(@PathVariable("taskId") Long taskId)
    {

        return success(taskService.selectTaskByTaskId(taskId));
    }

    /**
     * 新增【请填写功能名称】
     */
    @Transactional
    @PreAuthorize("@ss.hasPermi('system:task:add')")
    @Log(title = "增量数据添加", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Task task) {
        if (taskService.insertTask(task) != 0) {
            Long projectId = task.getProjectId();
            List<Progress> progresses = progressService.selectProgressList(new Progress());
            for (Progress progress : progresses) {
                if (projectId.equals(progress.getProjectId())) {
                    Long currentPercentage = progress.getProgressPercentage();
                    Long increment = task.getIncrement();
                    if (currentPercentage != null) {
                        currentPercentage += increment;
                    } else {
                        currentPercentage = increment;
                    }
                    progress.setProgressPercentage(currentPercentage);

                    if (currentPercentage >= 50 && "0".equals(progress.getNotification())) {
                        Project project = projectService.selectProjectByProjectId(projectId);
                        try {
                            sendSmsImpl.sendSms(project.getManagerName(), project.getContactInfo(), project.getProjectName());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        progress.setNotification("1");
                    }

                    progress.setProjectStatus(currentPercentage == 100 ? "3" : "2");
                    progressService.updateProgress(progress);
                }
            }
        }
        return success();
    }


    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:task:edit')")
    @Log(title = "增量数据更新", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Task task)
    {
        return toAjax(taskService.updateTask(task));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:task:remove')")
    @Log(title = "增量数据删除", businessType = BusinessType.DELETE)
	@DeleteMapping("/{taskIds}")
    public AjaxResult remove(@PathVariable Long[] taskIds)
    {
        return toAjax(taskService.deleteTaskByTaskIds(taskIds));
    }
}
